<?php

class Default_Model_Role extends Zend_Db_Table_Abstract
{
    protected $_name = 'role';
    
    public function getDataByUserId($userId){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.homepage','r.partnerId', 'r.gameId'))
                        ->join(array('ur'=>'user_role'),'r.roleId = ur.roleId', array('ur.userId'))
                        ->where('ur.userId = ?',(int)$userId);
                                        
    	return $this->fetchAll($query);
    }
    public function getDataByUserIdGameId($userId, $gameId){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.homepage','r.gameId','r.resourceId'))
                        ->joinLeft(array('ur'=>'user_role'), 'ur.roleId = r.roleId AND ur.userId = '.$userId, array())
                        ->where('r.gameId like ?',"%$gameId%");
                        
    	return $this->fetchRow($query);
    }
    
     public function getAllData(){
		$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('r'=>$this->_name),array('r.roleId','r.roleName','r.homepage','r.gameId','r.partnerId','r.resourceId'));
        
        return $this->fetchAll($query);
    }
    public function addData($roleName, $homepage, $gameId = '', $resourceId = '', $partnerId=''){
        $data = array('roleName' => $roleName, 'homepage' => $homepage, 'gameId' => $gameId, 'resourceId' => $resourceId, 'partnerId'=>$partnerId);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($roleId){
        $this->delete('roleId = '.(int)$roleId);
    }
    public function updateData($roleId, $roleName, $homepage, $gameId = '', $resourceId = '', $partnerId=''){
        $data = array('roleName' => $roleName, 'homepage' => $homepage, 'gameId' => $gameId, 'resourceId' => $resourceId, 'partnerId'=>$partnerId);
        return $this->update($data, 'roleId = '.(int) $roleId);
    }
    public function getDataByRoleName($roleName){
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.homepage','r.gameId','r.resourceId'))
                        ->where('r.roleName = ?',$roleName);
                        
    	return $this->fetchRow($query);
    }
    public function getDataByRoleId($roleid){  
        $query = $this->select()
                        ->from(array('r'=>$this->_name), array('r.roleId', 'r.roleName','r.homepage','r.gameId','r.resourceId', 'r.partnerId'))
                        ->where('r.roleId = ?',(int)$roleid);
                                        
    	return $this->fetchRow($query);
    }
   
    
}

























